package com.shibaobei.activity.controller;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2020/4/23 0023.
 * 流程变量
 *
 * 1.1 设置流程变量
 *     在启动流程实例时设置
 *     在办理
 *     使用RuntimeService的方法设置
 *     使用TaskService的方法设置
 *
 *
 *
 *
 *
 *
 *
 */
@RestController
@RequestMapping("/variable")
public class VariableController {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;

    /**
     * 设置流程变量方式一：在启动流程实例时设置
     */
    public void setVariableOne(){
        String processDefinitionKey = "";
        Map<String,Object> variable = new HashMap<String,Object>();
        variable.put("key1","value");
        variable.put("key2",1);


        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionKey, variable);


    }
    /**
     * 设置流程变量方式二：在办理任务时设置
     */
    public void setVariableTwo(){
       String taskId = "121";
        Map<String,Object> variable = new HashMap<String,Object>();
        variable.put("key1","value");
        variable.put("key2",1);
        taskService.complete(taskId,variable);

    }

    /**
     * 设置流程变量方式三：使用RuntimeService的方法设置
     */
    public void setVariableThree(){
        //执行id（流程实例id）
       String executionId = "";
       String variableName = "";
       String value = "";
       runtimeService.setVariable(executionId,variableName,value);

    }

    /**
     * 设置流程变量方式四：使用TaskService的方法设置
     */
    public void setVariableFour(){
        String taskId = "";
        Map<String,Object> variable = new HashMap<String,Object>();
        variable.put("key1","value");
        variable.put("key2",1);
        taskService.setVariables(taskId,variable);

    }

    /**
     * 获取流程变量方式一：使用RunTimeService的方法
     *
     */

    public void getVarable1(){
        String ececutionId = "";
        Map<String, Object> variables = runtimeService.getVariables(ececutionId);

        Object obj = runtimeService.getVariableLocal("ececutionId", "key");

        Collection<String> variablesNames = new ArrayList<>();
        Map<String, Object> map = runtimeService.getVariables("ececutionId", variablesNames);

    }

    /**
     * 获取流程变量方式二：使用TaskService的方法获取
     */
    public void getVarable_2(){
        Map<String, Object> map = taskService.getVariables("taskOd");

    }



}
